//
//  LSZSystemNotificationDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/9/12.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZSystemNotificationDetailsVC.h"

@interface LSZSystemNotificationDetailsVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewSystemNot;
@property(nonatomic,strong) NSMutableArray * arrayDataClass;
@property(nonatomic,assign) NSInteger page;
@end

@implementation LSZSystemNotificationDetailsVC
-(void)getClassificationData :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic detailsofthenotificationinterface:weakSelf.type kind:weakSelf.kind tag:weakSelf.tag page:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOk, NSString * string, LSZObtainNotificationDetailsManager * manager) {
        if(isOk == YES ){
            if (page == 1) {
                [weakSelf.arrayDataClass removeAllObjects];
                [weakSelf.arrayDataClass addObjectsFromArray:manager.arrayData];
                if (manager.arrayData.count == 0) {
                    weakSelf.tableViewSystemNot.mj_footer.hidden = YES;
                    LSZEmptyDataVC * emptyDataVC = [[LSZEmptyDataVC alloc]init];
                    [emptyDataVC showWithView:weakSelf.tableViewSystemNot];
                }
            }else{
                if (manager.arrayData.count == 0) {
                    [weakSelf.tableViewSystemNot.mj_footer endRefreshingWithNoMoreData];
                }
                [weakSelf.arrayDataClass addObjectsFromArray:manager.arrayData];
            }
            [self.tableViewSystemNot reloadData];
        }else{
            [self initWithtring:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"系统通知";
    self.arrayDataClass = [[NSMutableArray alloc]init];
    self.page = 1;
    [self getClassificationData:self.page];
    [self.tableViewSystemNot registerClass:[LSZSystemNotTableViewCell class] forCellReuseIdentifier:@"LSZSystemNotTableViewCell"];

    self.tableViewSystemNot.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getClassificationData :self.page];
            [self.tableViewSystemNot.mj_header endRefreshing];
        });
    }];
    self.tableViewSystemNot.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getClassificationData:self.page];
            [self.tableViewSystemNot.mj_footer endRefreshing];
        });
    }];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"全部阅读" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayDataClass.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:100 :5];
    return height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZSystemNotTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZSystemNotTableViewCell" forIndexPath:indexPath];
    LSZObtainNotificationDetailsMg * mg = nil;
    mg = self.arrayDataClass[indexPath.row];
    cell.obtainNotificationDetailsMg = mg;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZObtainNotificationDetailsMg * mg = nil;
    mg = self.arrayDataClass[indexPath.row];
    if ([mg.is_read isEqualToString:@"0"]) {
        [LSZRequestData readTheInformationInTheNotice:@"1" strID:mg.id_id :^(BOOL isOK, NSString * string, NSDictionary * diction) {
            if (isOK == YES) {
                [_UserM setSys_not_read:[NSString stringWithFormat:@"%ld",(long)[[_UserM sys_not_read] integerValue]-1]];
            }
            [self goToNextVC:mg.url];
        }];
    }else{
        [self goToNextVC:mg.url];
    }
}
-(void)goToNextVC:(NSString *)urlString
{
    WYWebController *webVC = [WYWebController new];
    webVC.url = urlString;
    [self.navigationController pushViewController:webVC animated:YES];
}
-(void)onClickedOKbtn
{
    if([[_UserM sys_not_read] integerValue] == 0){
        [self setStringWarning:@"全部已读!"];
    }else{
        [LSZRequestData readTheInformationInTheNotice:@"1" strID:@"" :^(BOOL isOK, NSString * string, NSDictionary * diction) {
            if (isOK == YES) {
                [_UserM setSys_not_read:@"0"];
                [self setStringWarning:@"已设置全部已读!"];
                for (int i = 0; i < self.arrayDataClass.count; i++) {
                    LSZObtainNotificationDetailsMg * mg = nil;
                    mg = self.arrayDataClass[i];
                    if ([mg.is_read isEqualToString:@"0"]) {
                        mg.is_read = @"1";
                    }
                }
                [self.tableViewSystemNot reloadData];
            }else{
                [self setStringWarning:string];
            }
        }];
    }
}
-(UITableView *)tableViewSystemNot
{
    if (_tableViewSystemNot == nil) {
        _tableViewSystemNot = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableViewSystemNot.delegate = self;
        _tableViewSystemNot.dataSource = self;
        _tableViewSystemNot.separatorColor = divider;
        _tableViewSystemNot.estimatedRowHeight =0;
        _tableViewSystemNot.estimatedSectionFooterHeight =0;
        _tableViewSystemNot.estimatedSectionHeaderHeight =0;
        _tableViewSystemNot.separatorInset = UIEdgeInsetsMake(0, 20, 20, 20);
        [self.view addSubview:_tableViewSystemNot];
    }
    return _tableViewSystemNot;
}

@end
